﻿using System;
using System.Collections.Generic;
using Core.Modal;
using Core.Helper;
using System.Data;
using Newtonsoft.Json.Linq;
using System.Text;

namespace Core.Core
{
    public class TagUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="project_id"></param>
        /// <param name="name"></param>
        /// <param name="comment"></param>
        /// <param name="tag_color"></param>
        /// <param name="tag_type"></param>
        /// <param name="tag_content"></param>
        /// <returns></returns>
        public static int Add(int project_id,string name, string comment, string tag_type, string tag_content, JArray docs =null)
        {
            int tagid= (int)DbHelper.ExecuteScalar($"exec proc_add_tag '{project_id}','{name}','{comment}','{tag_type}','{tag_content}'");
            if (docs != null && docs.Count > 0)
            {
                foreach (JValue doc in docs) {
                    PDTUtil.Add(project_id, Convert.ToInt32(doc), tagid);
                }
            }
            return tagid;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="project_id"></param>
        /// <param name="tag_id"></param>
        /// <param name="name"></param>
        /// <param name="comment"></param>
        /// <param name="tag_color"></param>
        /// <param name="tag_type"></param>
        /// <param name="tag_content"></param>
        public static void Update(int project_id,int tag_id, string name, string comment, string tag_type, string tag_content, JArray docs = null)
        {
            DbHelper.ExecuteNonQuery($"update Tag set tag_name='{name}', tag_comment='{comment}', tag_type='{tag_type}', tag_content='{tag_content}' where project_id='{project_id}' and tag_id='{tag_id}'");
            if (docs != null && docs.Count > 0) {
                PDTUtil.Delete(project_id, tag_id);
                foreach (JValue doc in docs)
                {
                    PDTUtil.Add(project_id, Convert.ToInt32(doc), tag_id);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectid"></param>
        /// <param name="tagid"></param>
        public static void Delete(int project_id, int tag_id)
        {
            DbHelper.ExecuteNonQuery($"delete from Tag where project_id='{project_id}' and tag_id='{tag_id}'");
            DbHelper.ExecuteNonQuery($"delete from PDTrelation where project_id='{project_id}' and tag_id='{tag_id}'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static modal_Tag Get(int project_id,int tag_id)
        {
            DataRow dr = DbHelper.ExecuteDataRow($"select project_id, tag_id, tag_name, tag_comment, tag_type, tag_content, create_date from Tag where project_id='{project_id}' and tag_id='{tag_id}'");
            if (dr != null)
            {
                modal_Tag obj = new modal_Tag();
                obj.create_date = Convert.ToDateTime(dr["create_date"].ToString());
                obj.project_id = Convert.ToInt32(dr["project_id"].ToString());
                obj.tag_id= Convert.ToInt32(dr["tag_id"].ToString());
                obj.tag_name = dr["tag_name"].ToString();
                obj.tag_comment = dr["tag_comment"].ToString();
                obj.tag_type= dr["tag_type"].ToString();
                obj.tag_content=dr["tag_content"].ToString();
                return obj;
            }
            return null;
        }

        public static IList<modal_Tag> GetList(string where, string orderby, int pageindex, int pagesize, ref int recordcount)
        {
            return DbHelper.PageData(orderby, pagesize, pageindex, "Tag", where, ref recordcount).ToList<modal_Tag>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="project_id"></param>
        /// <param name="doc_id"></param>
        /// <returns></returns>
        public static IList<modal_Tag> GetList(int project_id,int doc_id) {
            return DbHelper.ExecuteDataSet($"select a.project_id,a. tag_id, a.tag_name, a.tag_comment,a.tag_type, a.tag_content, a.create_date from Tag a inner join PDTrelation b on a. project_id=b.project_id and a.tag_id=b.tag_id where a.project_id='{project_id}' and b.doc_id='{doc_id}'").Tables[0].ToList<modal_Tag>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="project_id"></param>
        /// <param name="doc_id"></param>
        /// <returns></returns>
        public static string TagFitDocuments(int project_id, int tag_id)
        {
            StringBuilder sbDocs = new StringBuilder();
            IList<modal_Document> list = DbHelper.ExecuteDataSet($"select b.* from PDTrelation a inner join Document b on a.project_id=b.project_id and a.doc_id=b.doc_id where a.project_id='{project_id}' and a.tag_id='{tag_id}'").Tables[0].ToList<modal_Document>();
            for (int i = 0; i < list?.Count; i++)
            {
                sbDocs.AppendFormat("{0},", list[i]?.doc_name);
            }
            return sbDocs.ToString().TrimEnd(',');
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="project_id"></param>
        /// <param name="doc_id"></param>
        /// <returns></returns>
        public static IList<modal_Tag> GetListByDoc(int project_id, int doc_id,string where="") {
            return DbHelper.ExecuteDataSet($"select * from (select * from Tag where tag_type='common' and project_id='{project_id}' union all select a.* from Tag a inner join PDTrelation b on a.project_id=b.project_id and a.tag_id=b.tag_id where a.tag_type='exclusive' and b.project_id='{project_id}' and b.doc_id='{doc_id}')A where 1=1 {where}").Tables[0].ToList<modal_Tag>();
        }
    }
}
